package com.example.demo.iflytek;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.UUID;

/**
 * 文本纠错 res_id的生成、查询、删除
 * 运行前：请先填写Appid
 * 运行方法：直接运行 main() 即可
 * 结果： 控制台输出结果信息
 * 接口文档（必看）：https://www.xfyun.cn/doc/nlp/textCorrection/API.html
 *
 * @author iflytek
 */
public class ResIdGet {
    // 地址与鉴权信息
    public static final String hostUrl = "https://evo-gen.xfyun.cn/individuation/gen/upload";
    public static final String appid = WebTextCorrection.appid;
    // 个性化资源唯一标识，由字母、数字或下划线组成
    public static final String res_id = "XXX";
    // 黑名单
    public static final String black_list = "战士 展示,支撑 职称,规饭 鬼范,衣据 依据,轮到 论道";
    // 白名单
    public static final String white_list = "国务院,鱿鱼圈,画蛇天足";

    // 主函数
    public static void main(String[] args) {
        String json = getRequestJson();
        System.out.println(json);
        String backResult = doPostJson(hostUrl, json);
        System.out.println("上传资源返回结果：" + backResult);
    }

    // 请求参数json拼接
    public static String getRequestJson() {
        return "{\n" +
                "  \"common\": {\n" +
                "    \"app_id\": \"" + appid + "\",\n" +
                "    \"uid\": \"" + UUID.randomUUID() + "\"\n" +
                "  },\n" +
                "  \"business\": {\n" +
                "    \"res_id\": \"" + res_id + "\"\n" +
                "  },\n" +
                "  \"data\": \"" + getBase64TextData(black_list, white_list) + "\"\n" +
                "}";
    }

    // 读取文件
    public static String getBase64TextData(String black_list, String white_list) {
        String tempJson = "{\n" +
                "  \"black_list\":  \"" + black_list + "\",\n" +
                "  \"white_list\": \"" + white_list + "\"\n" +
                "}";
        return Base64.getEncoder().encodeToString(tempJson.getBytes());
    }

    // 根据json和url发起post请求
    public static String doPostJson(String url, String json) {
        CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
        CloseableHttpResponse closeableHttpResponse = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            // 创建请求内容
            StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
            httpPost.setEntity(entity);
            // 执行http请求
            closeableHttpResponse = closeableHttpClient.execute(httpPost);
            resultString = EntityUtils.toString(closeableHttpResponse.getEntity(), StandardCharsets.UTF_8);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (closeableHttpResponse != null) {
                    closeableHttpResponse.close();
                }
                if (closeableHttpClient != null) {
                    closeableHttpClient.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }
}